﻿Public Class Dimensiones

    Private oGuia As New CGuia
    Public idItemGuia As Integer
    Private dtDimensiones As New DataTable

    Private Sub Dimensiones_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.GetDimensiones()
    End Sub

    Private Sub GetDimensiones()
        Try
            Cursor = Cursors.WaitCursor
            Me.grdDimensiones.DataSource = Nothing
            Me.grdDimensiones.Columns.Clear()

            Dim ds As New DataSet
            ds = Me.oGuia.GetDimensiones(Me.idItemGuia)
            Me.dtDimensiones = ds.Tables(0)

            If (Me.dtDimensiones.Rows.Count > 0) Then
                Me.grdDimensiones.DataSource = Me.dtDimensiones
            Else
                'Se agrega una fila con ceros, para que salga algo en la grilla
                Dim dr As DataRow = Me.dtDimensiones.NewRow()
                dr("ID") = 0
                dr("ANCHO") = 0
                dr("ALTO") = 0
                dr("LARGO") = 0
                dr("CANTIDAD") = 0
                Me.dtDimensiones.Rows.Add(dr)
                Me.grdDimensiones.DataSource = Me.dtDimensiones
            End If

            Dim iconoELIMINAR As New DataGridViewImageColumn()
            iconoELIMINAR.ValuesAreIcons = False
            iconoELIMINAR.Image = My.Resources.eliminar
            iconoELIMINAR.HeaderText = "ELIMINAR"
            iconoELIMINAR.Name = "ELIMINAR"
            Me.grdDimensiones.Columns.Add(iconoELIMINAR)

            Me.grdDimensiones.Columns("ID").Visible = False
            Me.grdDimensiones.Columns("IDITEMGUIA").Visible = False
            Me.grdDimensiones.Columns("ELIMINAR").Width = 70


            Cursor = Cursors.Default
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("No se puedieron obtener las dimensiones: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub grdDimensiones_CellMouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdDimensiones.CellMouseEnter
        '*** Coloca la manito cuando el mouse entra en las celdas de editar y eliminar
        If (Me.grdDimensiones.Columns(e.ColumnIndex).Name = "ELIMINAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub grdDimensiones_CellMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdDimensiones.CellMouseMove
        If (Me.grdDimensiones.Columns(e.ColumnIndex).Name = "ELIMINAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub grdDimensiones_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdDimensiones.CellDoubleClick
        'Si se trata de la ultima fila no se hace nada
        If (Me.grdDimensiones.Rows(e.RowIndex).Cells("ID").Value Is DBNull.Value) Then
            Return
        End If

        'Se obtiene la info del registro
        Dim id As Integer = Me.grdDimensiones.Rows(e.RowIndex).Cells("ID").Value

        Select Case Me.grdDimensiones.Columns(e.ColumnIndex).Name
            Case "ELIMINAR"
                Me.EliminarDimension(e.RowIndex, id)
        End Select
    End Sub

    Private Sub EliminarDimension(ByVal indice As Integer, ByVal id As Integer)
        Try
            Cursor = Cursors.WaitCursor
            'Si el id es cero, simplemente se elimina del datatable porque aun no esta en la bd
            If (id > 0) Then
                Me.oGuia.EliminarDimension(id)
            End If
            Me.grdDimensiones.Rows.RemoveAt(indice)            
            Cursor = Cursors.Default
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("NO se eliminó la dimensión: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub btnListo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListo.Click
        'recorre la grilla y va actualizando los registros
        Try
            Cursor = Cursors.WaitCursor
            Dim id As Integer = 0
            Dim ancho As Double
            Dim largo As Double
            Dim alto As Double
            Dim cantidad As Integer
            Dim valorFinal As Double = 0
            Dim valorRegistro As Double = 0
            For i As Integer = 0 To Me.grdDimensiones.Rows.Count - 1
                If (Me.grdDimensiones.Rows(i).Cells("ID").Value IsNot Nothing) Then
                    If (Me.grdDimensiones.Rows(i).Cells("ID").Value.ToString().Length = 0) Then
                        id = 0
                    Else
                        id = Me.grdDimensiones.Rows(i).Cells("ID").Value.ToString()
                    End If

                    ancho = Me.grdDimensiones.Rows(i).Cells("ANCHO").Value.ToString()
                    largo = Me.grdDimensiones.Rows(i).Cells("LARGO").Value.ToString()
                    alto = Me.grdDimensiones.Rows(i).Cells("ALTO").Value.ToString()
                    cantidad = Me.grdDimensiones.Rows(i).Cells("CANTIDAD").Value.ToString()

                    Me.oGuia.ActualizarDimension(id, Me.idItemGuia, alto, ancho, largo, cantidad)

                    'Se calcula el valor del registro: ((ancho*alto*largo)/6000) * cantidad
                    valorRegistro = ((ancho * alto * largo) / 6000) * cantidad
                    valorFinal += valorRegistro
                End If
            Next

            If (My.Forms.Guias.Visible) Then
                My.Forms.Guias.chargeableWeightCalculado = valorFinal
                My.Forms.Guias.ActualizarChargeableWeight()
            End If

            Cursor = Cursors.Default

            Me.Close()
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("NO se actualizaron las dimensiones: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub

    Private Sub btnCancelarOtroCargo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelarOtroCargo.Click
        Me.Close()
    End Sub

    Private Sub grdDimensiones_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles grdDimensiones.DataError
        MessageBox.Show("Valor inválido", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub
End Class